package com.baizan.daka.ui.fragment

import android.graphics.Color
import com.baizan.daka.Constants
import com.baizan.daka.base.BaseListFragment
import com.baizan.daka.event.PayEvent
import com.baizan.daka.event.UserInfoEvent
import com.baizan.daka.glide.ImageLoadHelp
import com.baizan.daka.mvp.model.bean.UserInfoBean
import com.baizan.daka.mvp.model.bean.VipBean
import com.baizan.daka.ui.activity.FlowPackageActivity
import com.baizan.daka.ui.activity.PayActivity
import com.baizan.daka.ui.adapter.VipHomeAdapter
import com.baizan.daka.ui.contract.VipHomeContract
import com.baizan.daka.ui.presenter.VipHomePresenter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.frg_vip.*
import kotlinx.android.synthetic.main.frg_vip.user_account_tv
import kotlinx.android.synthetic.main.frg_vip.user_icon_iv
import kotlinx.android.synthetic.main.frg_vip.user_vip_tv
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VipFragment : BaseListFragment<VipHomePresenter, VipHomeAdapter>(), VipHomeContract.View{


    override fun initPresenter(): VipHomePresenter = VipHomePresenter(this)

    override fun layoutId(): Int = R.layout.frg_vip

    private fun initUserInfo(userInfoBean: UserInfoBean){
        Constants.userInfoBean = userInfoBean
        user_account_tv.text = if(userInfoBean.userName.isNullOrEmpty()) userInfoBean.account else userInfoBean.userName
        user_vip_tv.text = userInfoBean.vipLevelName
        ImageLoadHelp.loadImage(user_icon_iv, userInfoBean.headerUrl)
        when (userInfoBean.vipLevel) {
            0 -> user_vip_tv.setTextColor(Color.WHITE)
            1 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_1_color))
            2 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_2_color))
            3 -> user_vip_tv.setTextColor(resources.getColor(R.color.task_3_color))
            else -> user_vip_tv.setTextColor(resources.getColor(R.color.task_4_color))
        }
        adapter?.setUserVipLevel(userInfoBean.vipLevel)
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshVip(event: PayEvent) {
        presenter?.loadVipList()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun upOrDownEvent(event: UserInfoEvent) {
        Constants.userInfoBean?.let {
            initUserInfo(it)
        }
    }
    override fun initView() {

        flow_package.setOnClickListener {
            mContext?.let {
                FlowPackageActivity.start(it)
            }
        }
    }

    override fun initData() {
        super.initData()
        Constants.userInfoBean?.let {
            initUserInfo(it)
        } ?: let {
            presenter?.loadUserInfo()
        }
        adapter?.setOnItemClickListener { adapter, _, position ->
            val  list =adapter.data
            list as List<VipBean>
            mContext?.let {
                PayActivity.startPayVip(it, list[position])
            }
        }
    }
    override fun start() {
        EventBus.getDefault().register(this)
        presenter?.loadVipList()
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }
    override fun loadVipListSucess(data: List<VipBean>) {
        adapter?.setNewData(data)
    }

    override fun loadUserInfoSuccess(userInfoBean: UserInfoBean) {
        initUserInfo(userInfoBean)
    }

    override fun initAdapter(): VipHomeAdapter =VipHomeAdapter()

}